<template>
  <van-nav-bar
    left-arrow
    @click-left="onClickLeft"
    fixed
    :title="title"
    :right-text="rightText"
    @click-right="onClickRight"
  >
  </van-nav-bar>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
const router = useRouter()

const props = defineProps<{
  title?: string
  rightText?: string
  back?: () => void
}>()

const emits = defineEmits<{
  (e: 'click-right'): void
}>()

const onClickLeft = () => {
  // 如果传递了back函数
  if (props.back) {
    return props.back()
  }
  // 判断历史中有无记录
  if (history.state?.back) {
    router.back()
  } else {
    router.push('/')
  }
}

const onClickRight = () => {
  emits('click-right')
}
</script>

<style lang="scss" scoped>
::v-deep() {
  .van-nav-bar {
    &__arrow {
      font-size: 18px;
      color: var(--cp-text1);
    }

    &__text {
      font-size: 15px;
    }
  }
}
</style>
